//
//  NSDictionary+PropertyCode.m
//  07-Runtime(字典转模型KVC实现)
////
//  Created by xmg on 16/1/11.
//  Copyright © 2016年 xiaomage. All rights reserved.
//

#import "NSDictionary+PropertyCode.h"

// isKindOfClass:判断下是否是当前类或者子类

@implementation NSDictionary (PropertyCode)
// 自动生成,解析字典去生成,有多少key,就有多少个属性
- (void)propertyCode
{
    NSMutableString *codes = [NSMutableString string];
    
    // 遍历字典中所有key
    [self enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull value, BOOL * _Nonnull stop) {
        
        NSString *code;
        
        if ([value isKindOfClass:NSClassFromString(@"__NSCFBoolean")]) {
            
            code  = [NSString stringWithFormat:@"@property (nonatomic, assign) BOOL %@;",key];
            
        } else if ([value isKindOfClass:[NSString class]]) {
            
            code  = [NSString stringWithFormat:@"@property (nonatomic, strong) NSString *%@;",key];
            
        } else if ([value isKindOfClass:[NSArray class]]) {
            
            code  = [NSString stringWithFormat:@"@property (nonatomic, strong) NSArray *%@;",key];
            
        } else if ([value isKindOfClass:[NSDictionary class]]) {
            
            code  = [NSString stringWithFormat:@"@property (nonatomic, strong) NSDictionary *%@;",key];
            
        } else if ([value isKindOfClass:[NSNumber class]]) {
            
            code  = [NSString stringWithFormat:@"@property (nonatomic, assign) NSInteger %@;",key];
            
        }
        
        // 生成一行属性代码 reposts_count:@property (nonatomic, assign) NSInteger reposts_count;
        
        [codes appendFormat:@"\n/********* ***********/\n%@\n",code];
        
    }];
    
    NSLog(@"%@",codes);

}




- (NSMutableDictionary *)addMD5{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    params[@"timestamp"] = [NSString stringWithFormat:@"%@",@(time(NULL))];
    
    for (NSString *key in self.allKeys) {
        params[key] = self[key];
    }
  
    NSArray *keys = [params.allKeys sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        
        return [obj1 compare:obj2 options:NSLiteralSearch];
    }];
    
    
    NSMutableArray *arrayM = [NSMutableArray array];
    
    for (NSString *key in keys) {
        
        [arrayM addObject:[NSString stringWithFormat:@"%@=%@",key,params[key]]];
    }
    
    [arrayM addObject:@"bcc3c1df786c47e8"];
    
    
    params[@"sign"] = [[arrayM componentsJoinedByString:@"&"] md5String];
    
  
    
    return params;
    
}

@end
